package com.qf.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;

/**
 * projectName:spring_demo
 * author:liuXin
 * time:2022/7/8 16:50
 * description:
 */
public class DataSourceUtils {
    //创建数据源对象
    private static DruidDataSource dataSource;
    //初始化数据
    static{
        try {
            Properties properties=new Properties();
            InputStream is = DataSourceUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(is);
            dataSource= (DruidDataSource)DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException("初始化数据失败："+e.getMessage());
        }
    }
    public static DataSource getDataSource(){
        return dataSource;
    }
}
